<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Transaccion\Controller;

use Zend\Mvc\Controller\AbstractRestfulController;
use Transaccion\Model\Entity\DatosAcademicos;
use Transaccion\Model\Dao\DatosAcademicosDAO;
use Zend\View\Model\JsonModel;

class IndexController extends AbstractRestfulController
{
    public $dbAdapter;
    protected $alumnoTable;
    
    //http://localhost/UptaebBackend/public/maestros
    public function indexAction(){
        $id = $this->params()->fromRoute('id');
        echo "Seleccione un controlador  ";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        return $response;
    }
    
    public function getList(){
        $id = $this->params()->fromRoute('id');
        echo "getList ";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        return $response;
    }
    
     public function get($id){
        $id = $this->params()->fromRoute('id');
        echo "index get ";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        return $response;
    }
 
    public function create($data){
        $id = $this->params()->fromRoute('param1');
        echo "create ";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        return $response;
    }
 
    public function update($id, $data){
        $id = $this->params()->fromRoute('id');
        echo "update ".$id."_____-";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        //return $response;
        echo " ___".$id;
        return new JsonModel(array(
            'data' => $this->get($id),
        ));
    }
    
    public function replaceList($data){
        $id = $this->params()->fromRoute('param1');
        echo "replaceList ".$id."_____-   ";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        //return $response;
        return new JsonModel(array(
            'data' => $this->get($id),
        ));
    }
 
    public function delete($id){
        $id = $this->params()->fromRoute('param1');
        echo "delete ";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        return $response;
    }
    
    public function deleteList(){
        $id = $this->params()->fromRoute('param1');
        echo "delete list ";
        $registro_json = \Zend\Json\Json::encode($id, true);
        $response = $this->getResponseWithHeader();
        $response->getHeaders()->addHeaders(array(
            'Content-Type' => 'application/json',)
        );
        $response->setContent($registro_json);
        return $response;
    }
    
    public function getAlumnoTable(){
        if (!$this->alumnoTable) {
        $sm = $this->getServiceLocator();
        $this->alumnoTable = $sm->get('Transaccion\Model\Dao\AlumnoDAO');
        }
        return $this->alumnoTable;
    }
    
    /**
     * @return type
     */
    public function getResponseWithHeader()
    {
        $response = $this->getResponse();
        $response->getHeaders()
                //make can accessed by *  
                ->addHeaderLine('Access-Control-Allow-Origin','*')
                //set allow methods
                ->addHeaderLine('Access-Control-Allow-Methods','POST PUT DELETE GET');
        return $response;
    }
    
//    public function modelAction()
//    {
//        $m=new Modelo("texto parámetro");
//        $t=$m->getTexto();
//        $a=$m->getArray();
//        $desde=$m->desdeElController();
//        return new ViewModel(array('texto'=>$t,'array'=>$a,'desde'=>$desde));
//    }
}